//: Playground - noun: a place where people can play

import UIKit

// 定义一个变量暂时不需要使用
var name:String?

let name1:String?
name1 = "hello";

class Person {
    private var name = ""
    public var height = 0.0;
    // 可选类型
    public var firstName:Optional<String>;
    // 可选类型语法糖
    public var lastName:String?
    
}

var p = Person()
//p.name;
p.height

// 给可选类型赋值
p.firstName = "yutang";
p.lastName = "zhang"

// 取出可选类型的值
print(p.firstName!)//可选类型的强制解包!!!如果强制解包的值为nil会crash
if (p.firstName != nil) {
    print(p.firstName!)//可选类型的强制解包!!!如果强制解包的值为nil会crash
}

// 可选绑定,定义一个临时变量绑定到原始变量，并且把原始变量解包
if let lastName = p.lastName {
    print(lastName)
}

// 可选绑定需要用于可选类型
//if let height = p.height {
//    print(height)
//}

// 可选类型的应用场景
let string: String?
string = "你好";
let url: NSURL! = NSURL(string: string!);

// 可选类型的解析
if let url = url {
    print("==");
}else {
    print("XX")
}









